from rest_framework.decorators import action
from rest_framework.response import Response
from rest_framework.viewsets import ModelViewSet

from wch_file_storage.models import FileModel
from wch_file_storage.serializers import ImageFileModelSerializerStrategy


# ffmpeg -i input.mp4 -vcodec h264 output.mp4

# ----------ImageFileModel view set start----------
class ImageFileModelViewSet(ModelViewSet):
    """
    create:
        创建
    """
    serializer_class = ImageFileModelSerializerStrategy
    ordering = None

    lookup_field = 'uuid'

    def create(self, request, *args, **kwargs):
        print(request.data)
        # return Response(
        #     {'result_type': 'image',
        #      'data': ['http://127.0.0.1:9000/a.jpg', 'http://127.0.0.1:9000/a.jpg', 'http://127.0.0.1:9000/a.jpg',
        #               'http://127.0.0.1:9000/a.jpg']})

        return Response(
            {'result_type': 'video',
             'data': [{'index': a, 'src': b} for a, b in enumerate([
                 'http://127.0.0.1:9000/1.mp4',
                 'http://127.0.0.1:9000/1.mp4',
                 'http://127.0.0.1:9000/1.mp4',
                 'http://127.0.0.1:9000/1.mp4',
                 'http://127.0.0.1:9000/1.mp4',
             ])]})
    @action(
        methods=['POST'],
        detail=False,
        url_path='video',
    )
    def video(self, request, uuid=None):

        mp4_file = request.FILES.get('file')
        # file = open('./reid_web/app/static/img/p_img.png', 'wb')
        fobj = open('./p_mp4.mp4', 'wb')
        for chrunk in mp4_file.chunks():
            fobj.write(chrunk)
        fobj.close()

        return Response(
            [
                'http://127.0.0.1:9000/1.mp4',
                'http://127.0.0.1:9000/1.mp4',
                'http://127.0.0.1:9000/1.mp4',
                'http://127.0.0.1:9000/1.mp4',
                'http://127.0.0.1:9000/1.mp4',
            ])

# ----------ImageFileModel view set end----------
